myApp_.onPageInit('address',function(page){
	//是否是从订单详情过来选择地址
	if(page.fromPage.name == 'make_order'){
		ADDRESS.is_check_addr = true;
	}
	ADDRESS.request_();
});
myApp_.onPageAfterAnimation('address', function (page) {
	if(page.from == 'left'){//back时  from=left
		ADDRESS.request_();
	}
});

ADDRESS = {};
ADDRESS.is_check_addr = false;

ADDRESS.__INIT__ = function(){
	mainView.router.load({
		url: 'src/my_center/address/address.html'
	});
}

ADDRESS.request_ = function(){
	request_(
		url_('api/V4/address_list')
		,{
			community_id: SESSION.get_community_id()
		}
		,function(d){
			if(d.code == 0){
				ADDRESS.fill_list(d.data);
			}else{
				my_tips_(d.msg);
			}
		}
	);
}

ADDRESS.fill_list = function(d){
	$.tpl({
		url: 'src/my_center/address/address_tpl.html'
		,context: d
		,elem: 'ul#address_content'
	});
}


/**
 * ========================================地址编辑操作========================================
 */
//设置默认地址,或者选择地址
ADDRESS.set_default = function(id,t){
	if(ADDRESS.is_check_addr){
		//设置选择地址
		ADDRESS.set_addr(t);
	}else{
		//设置默认地址
		myApp_.confirm('您确定要将此地址设为默认地址吗？',function(){
			request_(
				url_('api/V4/address_default')
				,{
					address_id: id
				}
				,function(d){
					if(d.code == 0){
						ADDRESS.request_();		
					}else{
						my_tips_(d.msg);
					}
				}
			);
		});
	}
}
//选择地址
ADDRESS.set_addr = function(t){
	var name = $(t).parents('li.swipeout').find('div.addr_name span').html();
	var tel = $(t).parents('li.swipeout').find('div.addr_tel span').html();
	var addr = $(t).parents('li.swipeout').find('div.addr_detail span').html();
	var id = $(t).parents('li.swipeout').data('id');
	
	//更改数据 重新渲染
	var h = sprintf(
			'<p class="shouhuo_uname" >姓名：%s</p>'+
		  	'<p class="shouhuo_tel">%s</p>'+
		  	'<p class="shouhuo_area" data-address_id="%s">收货地址：%s</p>'
		  	,name
		  	,tel
		  	,id
		  	,addr
		);
	$('.make_order_od_inner .od_padding').html(h);
	mainView.router.back();
}




//提交地址信息
ADDRESS.submit_adress = function(id){
	//获取信息
	var user_name = $('input.addrname').val();
	var telephone = $('input.addrtel').val();
	var address = $('textarea.address').val();
	//检测信息是否合法
	if(!user_name){
		my_tips_('请输入联系人姓名');
		return false;
	}
	if(!telephone){
		my_tips_('请输入联系人电话');
		return false;
	}
	if(!address){
		my_tips_('请输入详细地址');
		return false;
	}
	//传id就是编辑  不传id就是新添
	if(id){
		var data = {
			op: 'edit'
			,address_id: id
			,community_id: SESSION.get_community_id()
			,user_name: user_name
			,telephone: telephone
			,address: address
		}
	}else{
		var data = {
			op: 'edit'
			,community_id: SESSION.get_community_id()
			,user_name: user_name
			,telephone: telephone
			,address: address
		}
	}
	//请求
	request_(
		url_('api/V4/address_edit')
		,data
		,function(d){
			if(d.code == 0){
				mainView.router.back();
			}else{
				my_tips_(d.msg);
			}
		}
	);
	
}
//添加
ADDRESS.add = function(){
	mainView.router.load({
		url: 'src/my_center/address/address_edit.html'
	});
}

//修改
ADDRESS.edit = function(t){
	//获取地址信息
	var d = {};
	d.name = $(t).parents('li.swipeout').find('div.addr_name span').html();
	d.tel = $(t).parents('li.swipeout').find('div.addr_tel span').html();
	d.addr = $(t).parents('li.swipeout').find('div.addr_detail span').html();
	d.id = $(t).parents('li.swipeout').data('id');
	
	mainView.router.load({
		url: 'src/my_center/address/address_edit.html'
		,context: d
	});
}

//删除
ADDRESS.del = function(id){
	myApp_.confirm('确定要删除此地址吗？',function(){
		request_(
			url_('api/V4/address_edit')
			,{
				op: 'del'
				,address_id: id
				,community_id: SESSION.get_community_id()
			}
			,function(d){
				if(d.code == 0){
                    ADDRESS.request_();
				}else{
					my_tips_(d.msg);
				}
			}
		);
	});
}

